#include "ChademoReceiveProcess.h"
#include "stdio.h"
#include "inputCtl.h"
#include "outputCtl.h"
#include "ChargeCtrl.h"
#include "uuPowerProcess.h"
#include "ccsLogicProcess.h"


void CHAdeMO_ReportInitMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == ReportInitRes_Len)
	{
		p_rx->errorCode = frame[4];
		p_rx->majorVersion = frame[8];
		p_rx->minorVersion = frame[9];
		p_rx->releaseVersion = frame[10];
		p_rx->infoLen = frame[11];
		for(i=0;i<24;i++)
		{
			p_rx->rptInitInfo[i] = frame[12+i];
		}
		commCtrl->rcvFlag.flagBit.rptInit = 1;//ReportInitRes接收标志
   
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxInit_Index].rx_Overtime_Mark = 0;//清除ReportInitRes接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxInit_Index].rx_CountDown = RxOverTime[CCS_RxInit_Index].rx_CountDown;//ReportInitInitRes帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxInit_Index].rx_Overtime_Enable = DISABLE;
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("ReportInitRes: ");
		for(i = 0; i < ReportInitRes_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		if(p_rx->errorCode)
		{
			printf("ReportInitRes Error Code:%02X\n", p_rx->errorCode);
		}
		#endif

	}
}



void CHAdeMO_ReportSLACMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == ReportSLAC_Len)
	{
		p_rx->errorCode = frame[4];
		p_rx->aveAtten = frame[8];
		for(i=0;i<6;i++)
		{
			p_rx->pevMac[i] = frame[10+i];
		}
		commCtrl->rcvFlag.flagBit.rptSlac  = 1;		//设置接收 ReportSLAC 标志
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("ReportSLAC: ");
		for(i = 0; i < ReportSLAC_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		if(p_rx->errorCode)
		{
			printf("ReportSLAC Error Code: %02X\n", p_rx->errorCode);
		}
		#endif
	}
}

void CHAdeMO_ReportSDPMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == ReportSDP_Len)
	{
		p_rx->errorCode = frame[4];
		p_rx->tcpPort = ((uint16_t)frame[9]<<8) | ((uint16_t)frame[8]);
		p_rx->secData = frame[10];
		p_rx->tcpData = frame[11];
	}
	commCtrl->rcvFlag.flagBit.rptSdp = 1;
	
	//plc接收报文日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
	printf("ReportSDP: ");
	for(i = 0; i < ReportSDP_Len+8; i++)
	{
		printf("%02X ",frame[i]);
	}
	printf("\n");
	#endif
//	if(p_rx->errorCode)
//	{
//		// SDP 报错
//		printf("ReportSDP Error Code: %02X\n", p_rx->errorCode);
//		RNL_SetChademoChargingStep(CHADEMO_ASTOP, gunIndex);
//		p_rx->errorCode = 0;
//	}

}

void CHAdeMO_ReportV2GCHAMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == ReportV2GCHA_Len)
	{
		p_rx->errorCode = frame[4];
	}
//	commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxRPTV2G_Index].rx_Flag  = 1;	//设置接收 ReportV2GCHA 标志
	commCtrl->rcvFlag.flagBit.rptV2g = 1;
	
	//plc接收报文日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
	printf("ReportV2G/CHAdeMO/AC: ");
	for(i = 0; i < ReportV2GCHA_Len+8; i++)
	{
		printf("%02X ",frame[i]);
	}
	printf("\n");
	#endif
	
//	if(p_rx->errorCode)
//	{
//		printf("ReportV2G/CHAdeMO/AC Error Code: %02X\n", p_rx->errorCode);
//		RNL_SetChademoChargingStep(CHADEMO_ASTOP, gunIndex);
//		p_rx->errorCode = 0;
//	}
}



void CHAdeMO_AllStopMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == AllStopRes_Len)
	{
		for(i=0;i<4;i++)
		{
			p_rx->astInfo[i] = frame[3+i];
		}
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxAllStop_Index].rx_Overtime_Mark = 0;//清除AllStop接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxAllStop_Index].rx_CountDown = RxOverTime[CCS_RxAllStop_Index].rx_CountDown;//AllStop帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxAllStop_Index].rx_Overtime_Enable = DISABLE;
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("AllStopRes: ");
		for(i = 0; i < AllStopRes_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		commCtrl->rcvFlag.flagBit.aStop = 1;//AllStopRes接收消息标志位置1
		#endif

	}
}



void CHAdeMO_ReportStateMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == ReportStateRes_Len)
	{
		 p_rx->rptStateType = frame[4];
		for(i=0;i<3;i++)
		{
			p_rx->rptStateInfo[i] = frame[5+i];
		}
		switch(p_rx->rptStateType)
		{
			case CP_State:
				//CP Voltage
				if((p_rx->rptStateInfo[0]>7)&&(p_rx->rptStateInfo[0]<10))
				{
					if(getShortPressSignal(AC_CONTACT_STATUS)==0)
					{
						setKeyValueHandle(AC_CONTROL_MODULE,1,0,0x00,0x00,NULL);
					}
				}
				break;
			case CHAdeMO_LockState:
				#if defined (__RNL_CHADEMO_DEBUG__)
				printf("CHAdeMO Lock state = ");
				if(p_rx->rptStateInfo[0])
				{
					printf("plug-in\n");
				}else
				{
					printf("unplugged\n");
				}
				#endif
				break;
			case CHAdeMO_PluginState:
				printf("CHAdeMO plug-in state = ");
				if(p_rx->rptStateInfo[0])
				{
					printf("lock\n");
					if(getShortPressSignal(AC_CONTACT_STATUS)==0)
					{
						setKeyValueHandle(AC_CONTROL_MODULE,1,0,0x00,0x00,NULL);
						p_rx->chademoPlugin = 1;
					}
				}else
				{
					printf("unlock\n");
				}
				break;
			case Paused_Restart_Request:
				#if defined (__RNL_CHADEMO_DEBUG__)
				if(p_rx->rptStateInfo[0]==0)
				{
					printf("pause disabled\n");
				}else if(p_rx->rptStateInfo[0]==1)
				{
					printf("pause enabled\n");
				}else if(p_rx->rptStateInfo[0]==2)
				{
					printf("Restart Request\n");
				}
				#endif
				break;
			default:
				break;
		}
		
		commCtrl->rcvFlag.flagBit.rptState = 1;			//设置接收 ReportState 标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxRPTState_Index].rx_Overtime_Mark = 0;	//清除 ReportState 接收超时标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxRPTState_Index].rx_CountDown = RxOverTime[CCS_RxRPTState_Index].rx_CountDown;//刷新 ReportStateRes 帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxRPTState_Index].rx_Overtime_Enable = DISABLE;
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("ReportState: ");
		for(i = 0; i < ReportStateRes_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n"); 
		#endif
		
	}
}



void CHAdeMO_SessionSetupMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == CHADEMO_SessionSetupReq_Len)
	{
		p_rx->evStatus.ErrorCode = frame[4];
		commCtrl->rcvFlag.flagBit.sSetup = 1;		//设置接收SessionSetupReq 标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxSSetUp_Index].rx_Overtime_Mark = 0;//清除SessionSetupReq超时标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxSSetUp_Index].rx_CountDown = RxOverTime[CCS_RxSSetUp_Index].rx_CountDown;//SessionSetupReq帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxSSetUp_Index].rx_Overtime_Enable = DISABLE;		
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("SessionSetupReq: ");
		for(i = 0; i < CHADEMO_SessionSetupReq_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		#endif

		
	}
}
 

/*
*   函 数 名: dataFormatChange
*   功能说明: 把输入的符号数转换为无符号的数
*   形    参: DataIn:输入的符号数
              p(0):0表示正数  1表示负数
              p(1):表示数值
*/
void dataFormatChange(uint8_t data, uint16_t *p)
{
    if(data == 0xFF)    //-1,缩小10倍
    {
        *(p) = 1;       //负数
        *(p+1) = 10;    //除以10
    }
    else if(data == 0xFE)    //-2,缩小100倍
    {
        *(p) = 1;       //负数
        *(p+1) = 100;   //除以100
    }
    else if(data == 0xFD)    //-3,缩小1000倍
    {
        *(p) = 1;       //负数
        *(p+1) = 1000;    //除以1000
    }
    else if(data == 0xFC)    //-4,缩小10000倍
    {
        *(p) = 1;       //负数
        *(p+1) = 10000;    //除以10000
    }
    else if(data == 1)    //1,放大10倍
    {
        *(p) = 0;       //正数
        *(p+1) = 10;    //乘以10
    }
    else if(data == 2)    //2,放大100倍
    {
        *(p) = 0;       //正数
        *(p+1) = 100;    //乘以100
    }
    else if(data == 3)    //3,放大1000倍
    {
        *(p) = 0;       //正数
        *(p+1) = 1000;    //乘以1000
    }
    else if(data == 4)    //4,放大10000倍
    {
        *(p) = 0;       //正数
        *(p+1) = 10000;    //乘以10000
    }
    else
    {
        //当成0处理，也就是没有缩小和放大
        *(p) = 0;       //正数
        *(p+1) = 1;     //乘以1
    }
}

void CHAdeMO_ChargeParameterDiscoveryMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	uint16_t temp;
	uint16_t dataT[2];
	if(frame[3] == CHADEMO_ChargeParameterDiscoveryReq_Len)
	{
		p_rx->evStatus.ReadyFlag = frame[4];
		p_rx->evStatus.ErrorCode = frame[5];
		p_rx->evStatus.RESSSOC = frame[6];
		//EV最大电压限制
		temp = ((uint16_t)frame[11]<<8) | ((uint16_t)frame[10]);
		dataFormatChange(frame[8], dataT);
		if(dataT[0]==1)
		{
			p_rx->evMaxVol = temp / dataT[1];
		}
		else
		{
			p_rx->evMaxVol = temp * dataT[1];
		}
		
		commCtrl->rcvFlag.flagBit.chrgParaDisc = 1; 	//设置接收 ChargeParameterDiscoveryReq 标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxChgParaDisc_Index].rx_Overtime_Mark = 0;//清除ChargeParameterDiscoveryReq接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxChgParaDisc_Index].rx_CountDown = RxOverTime[CCS_RxChgParaDisc_Index].rx_CountDown;//ChargeParameterDiscoveryReq帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxChgParaDisc_Index].rx_Overtime_Enable = DISABLE;

		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("ChargeParameterDiscoveryReq: ");
		for(i = 0; i < CHADEMO_ChargeParameterDiscoveryReq_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");

		if(p_rx->evStatus.ErrorCode)
		{
			printf("ChargeParameterDiscovery ErrorCode: %02X\n", p_rx->evStatus.ErrorCode);
		}
		#endif
	}
}


void CHAdeMO_PowerDeliveryMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == CHADEMO_PowerDeliveryReq_Len)
	{
		p_rx->rdyChgState = frame[4];
		p_rx->evStatus.ErrorCode = frame[5];

		commCtrl->rcvFlag.flagBit.powerdeliv = 1;	// 设置 PowerDeliveryReq 接收标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxPowerDeliv_Index].rx_Overtime_Mark = 0;// 清除PowerDelivery接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxPowerDeliv_Index].rx_CountDown = RxOverTime[CCS_RxPowerDeliv_Index].rx_CountDown;//PowerDelivery帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxPowerDeliv_Index].rx_Overtime_Enable = DISABLE;
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("PowerDeliveryReq: ");
		for(i = 0; i < CHADEMO_PowerDeliveryReq_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		if(p_rx->evStatus.ErrorCode)
		{
			printf("PowerDeliveryReq ErrorCode: %02X\n", p_rx->evStatus.ErrorCode);
		}
		#endif
	}
}

	
void CHAdeMO_CableCheckMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == CHADEMO_CableCheckReq_Len)
	{
		p_rx->evStatus.ReadyFlag = frame[4];
		p_rx->evStatus.ErrorCode = frame[5];
		p_rx->evStatus.RESSSOC = frame[6];
		commCtrl->rcvFlag.flagBit.cableChk = 1;	// 设置 CableCheckReq 接收标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxCableChk_Index].rx_Overtime_Mark = 0;// 清除CableCheckReq接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxCableChk_Index].rx_CountDown = RxOverTime[CCS_RxCableChk_Index].rx_CountDown;//CableCheckReq帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxCableChk_Index].rx_Overtime_Enable = DISABLE;
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("CableCheckReq: ");
		for(i = 0; i < CHADEMO_CableCheckReq_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		if(p_rx->evStatus.ErrorCode)
		{
			printf("CableCheckReq ErrorCode: %02X\n", p_rx->evStatus.ErrorCode);
		}
		#endif
	}
}

void CHAdeMO_CurrendDemandMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	uint16_t temp;
	uint16_t dataT[2];
	if(frame[3] == CHADEMO_CurrentDemandReq_Len)
	{
		p_rx->evStatus.ReadyFlag = frame[4];
		p_rx->evStatus.ErrorCode = frame[5];
		p_rx->evStatus.RESSSOC = frame[6];
		// 目标电流
		temp = ((uint16_t)frame[11] << 8) | ((uint16_t)frame[10]);
		dataFormatChange(frame[8], dataT);
		if(dataT[0] == 1) { 
			p_rx->evTargetCur = temp / dataT[1];
		} else {
			p_rx->evTargetCur = temp * dataT[1];
		}
		//RemainingTimeToFullSoC
		p_rx->remainTimeFullSOC = (((uint32_t)frame[15] << 24) | ((uint32_t)frame[14] << 16) |
		                           ((uint32_t)frame[13] << 8) | (uint32_t)frame[12]);
		// 目标电压
		temp = ((uint16_t)frame[19] << 8) | ((uint16_t)frame[18]);
		dataFormatChange(frame[16], dataT);
		if(dataT[0] == 1) {
			p_rx->evTargetVol = temp / dataT[1];
		} else {
			p_rx->evTargetVol = temp * dataT[1];
		}
		
		commCtrl->rcvFlag.flagBit.curDemd = 1;	// 设置 CurrendDemandReq 接收标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxCurDemand_Index].rx_Overtime_Mark = 0;// 清除CurrendDemandReq接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxCurDemand_Index].rx_CountDown = EVSE_232_RXCURDMD_TIMEOUT;// 刷新CurrendDemandReq 帧接收超时时间
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxCurDemand_Index].rx_Overtime_Enable = DISABLE;
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("CurrentDemandReq: ");
		for(i = 0; i < CHADEMO_CurrentDemandReq_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		if(p_rx->evStatus.ErrorCode)
		{
			printf("CurrentDemandReq ErrorCode: %02X\n", p_rx->evStatus.ErrorCode);
		}
		#endif
	}
}



void CHAdeMO_WeldingDetectionMsg_Analyse( unsigned char *frame, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == CHADEMO_WeldingDetectionReq_Len)
	{
		p_rx->evStatus.ErrorCode = frame[4];
		
		commCtrl->rcvFlag.flagBit.weldDet = 1;
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxWeldingDetect_Index].rx_Overtime_Mark = 0;// 清除WeldingDetectionReq接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxWeldingDetect_Index].rx_Overtime_Enable = DISABLE;
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxWeldingDetect_Index].rx_CountDown = RxOverTime[CCS_RxWeldingDetect_Index].rx_CountDown;// 刷新WeldingDetectionReq帧接收超时时间
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("WeldingDetectionReq: ");
		for(i = 0; i < CHADEMO_WeldingDetectionReq_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		if(p_rx->evStatus.ErrorCode)
		{
			printf("WeldingDetectionReq ErrorCode: %02X\n", p_rx->evStatus.ErrorCode);
		}
		#endif
	}
}


void CHAdeMO_SessionStopMsg_Analyse(uint8_t *frame, RNL_CHAdeMOEvsePara *p_tx, RNL_CHAdeMOEvccPara *p_rx, RNL_CHAdeMOCommunicationCtrlS *commCtrl)
{
	uint8_t i;
	if(frame[3] == CHADEMO_SessionStopReq_Len)
	{
		p_rx->evStatus.ErrorCode = frame[4];
		
		commCtrl->rcvFlag.flagBit.sStop = 1;	// 设置 SessionStopReq 接收标志
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxSStop_Index].rx_Overtime_Mark = 0;// 清除SessionStopReq接收超时
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxSStop_Index].rx_Overtime_Enable = DISABLE;
//		commCtrl->Rx_Msg_OverTime_Ctrl[CCS_RxSStop_Index].rx_CountDown = RxOverTime[CCS_RxSStop_Index].rx_CountDown;// 刷新SessionStopReq帧接收超时时间
		
		//plc接收报文日志打印
		#if defined (__RNL_CHADEMO_DEBUG__)
		printf("SessionStopReq: ");
		for(i = 0; i < CHADEMO_SessionStopReq_Len+8; i++)
		{
			printf("%02X ",frame[i]);
		}
		printf("\n");
		#endif
	}
}

// 解包:Signature	MsgID	Body	CheckSum
uint8_t CHAdeMORs232MsgUnpack(uint8_t *frame, uint8_t gunId)
{
	uint8_t msgID = 0;
	uint8_t msgLen,totalLen;
	uint8_t checksum[4];
	uint8_t checkSunTemp[4];

	uint8_t i;
	uint32_t chksum = 0;
	uint32_t pCurrent;	// 辅助指针
	//1.解包条件：队列中消息的长度>转换板发出的包的最小长度
	//2.找GQ：循环每次读1个字节直到找到GQ
	while((plc232_RxQue[gunId].length >= RNL_CHADEMO_PLCMSG_MINLEN) && msgID == 0)
	{
		pCurrent = plc232_RxQue[gunId].tail;
		if((plc232_RxQue[gunId].rcvData[pCurrent] == 'G') && (plc232_RxQue[gunId].rcvData[(pCurrent+1) % MAX_RCVDATA] == 'Q'))
		{
			//3.成功读到GQ，再读出MsgID，MsgLen
			msgID = plc232_RxQue[gunId].rcvData[(pCurrent+2) % MAX_RCVDATA];
			msgLen = plc232_RxQue[gunId].rcvData[(pCurrent+3) % MAX_RCVDATA];
			totalLen = msgLen + 8;
			
			//4.根据MsgLen计算这一帧的校验和
			if(plc232_RxQue[gunId].length >= totalLen && msgID>0)
			{
				for(i = 0; i < msgLen; i++)
				{
					chksum += plc232_RxQue[gunId].rcvData[(pCurrent+4+i) % MAX_RCVDATA];
				}
				//5.将Checksum从队列中读出并判断和校验是否正确
				for(i=0;i< 4 ;i++)
				{
					checksum[i] = plc232_RxQue[gunId].rcvData[(pCurrent+msgLen+4+i) % MAX_RCVDATA];
				}
				if(msgLen > 0)
				{
					checkSunTemp[0] = chksum&0xff;
					checkSunTemp[1] = (chksum>>8)&0xff;
					checkSunTemp[2] = (chksum>>16)&0xff;
					checkSunTemp[3] = (chksum>>24)&0xff;
					if((checksum[0] == checkSunTemp[0])	&& (checksum[1] == checkSunTemp[1])
							&& (checksum[2] == checkSunTemp[2])	&& (checksum[3] == checkSunTemp[3]))
					{	
						// 6.校验和正确,从队列中取出这帧数据
						getQueueData(&plc232_RxQue[gunId], frame, totalLen);
						break;	// 解出一帧数据就传出去解析
					}
					else
					{	
						#if defined (__RNL_CHADEMO_DEBUG__)
						switch(gunId)
						{
							case GUNB:
								printf("GUNB ");
							break;
							case GUNA:
								printf("GUNA ");
							break;
							default:
								break;
						}
						printf("Message Id %02X Check sum error!\n ",msgID);
						#endif
						return 0;// 校验和不正确，通讯出错
					}
				}
				else
				{
					getQueueData(&plc232_RxQue[gunId], frame, totalLen);
					break;
				}// 校验和为0可直接退出循环
			}
			else
			{
				return 0;
			}// 未接收完成直接退出解包
		}
		else
		{
			queuePop(&plc232_RxQue[gunId]);	// 没找到'GQ'，移除消息队列中的1个字节，辅助指针=新tail
		}
	}
	return msgID;
}

